package com.aruba.controller;

import com.aruba.bean.Emp;
import com.aruba.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
public class EmpController {
    @Autowired
    private EmpService empService;

    @RequestMapping("findAllEmp")
    @ResponseBody
    public List<Emp> findAllEmp() {
        return empService.findAllEmp();
    }

    @RequestMapping("findEmpByPage/{pageSize}/{pageNum}")
    @ResponseBody
    public List<Emp> findEmpByPage(@PathVariable("pageSize") Integer pageSize,
                                   @PathVariable("pageNum") Integer pageNum) {
        return empService.findEmpByPage(pageSize, pageNum);
    }


    @RequestMapping("showEmpList")
    public ModelAndView showEmpList(ModelAndView modelAndView) {
        List<Emp> allEmpList = empService.findAllEmp();
        modelAndView.setViewName("showEmpList");
        Map<String, Object> model = modelAndView.getModel();
        model.put("empList", allEmpList);

        return modelAndView;
    }

    @RequestMapping("showEmpMap")
    public ModelAndView showEmpMap(ModelAndView modelAndView) {
        List<Emp> allEmp = empService.findAllEmp();

        Map<String, Emp> empMap = new HashMap<>();
        for (Emp e : allEmp) {
            empMap.put(e.getEmpno().toString(), e);
        }

        modelAndView.setViewName("showEmpMap");
        Map<String, Object> model = modelAndView.getModel();
        model.put("empMap", empMap);
        System.out.println(empMap);
        return modelAndView;
    }

}
